function [calibAmp, calibFreq, calibPhase] = simpleCalib(din, calibCfg)
% size(din) = NUM_SAMP * NUM_ANT
fig    = calibCfg.figStart;
ptRange= calibCfg.ptRange;
numAnt = calibCfg.numAnt;
ptTimes= calibCfg.ptTimes;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% original time domain data
figure(fig); fig=fig+1;
for k=1:numAnt
    plot(real(din(:, k))); hold on;
end
hold off;
title('time domain data');
legend('1','2','3','4','5','6','7','8','9','10','11','12');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dfft = fft(din, ptTimes * ptRange, 1);
dabs = abs(dfft(1:ptTimes * ptRange/2,:));
figure(fig); fig=fig+1;
for k=1:numAnt
    plot(dabs(:, k)); hold on;
end
hold off;
title('freq domain before calib');
legend('1','2','3','4','5','6','7','8','9','10','11','12');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get calib freq & apply data
for k=1:numAnt
    [max_val(k), max_f(k)] = max(dabs(1:end,k));
%     max_f(k)=max_f(k)+ptTimes*10;
    calibFreq(k) = (max_f(1) - max_f(k)) / ptTimes;
end
for k=1:numAnt
    din_calibFreq(:, k) = din(:, k) .* exp(1j * 2 * pi * calibFreq(k) * [0:ptRange-1]/ptRange).';
end

dfft = fft(din_calibFreq, ptTimes * ptRange, 1);
dabs = abs(dfft);
figure(fig); fig=fig+1;
for k=1:numAnt
    plot(dabs(:, k)); hold on;
end
hold off;
title('freq domain after calib freq');
legend('1','2','3','4','5','6','7','8','9','10','11','12');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get calib amp & apply data
dtmp = dabs(max_f(1),:);
for k=1:numAnt
    calibAmp(k) = dtmp(1) / dtmp(k);
end

for k=1:numAnt
    din_calibFreqAmp(:, k) = din_calibFreq(:, k) * calibAmp(k);
end
dfft = fft(din_calibFreqAmp, ptTimes * ptRange, 1);
dabs = abs(dfft);
figure(fig); fig=fig+1;
for k=1:numAnt
    plot(dabs(:, k)); hold on;
end
hold off;
title('freq domain after calib amp');
legend('1','2','3','4','5','6','7','8','9','10','11','12');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get calib phase & apply data
dtmp = angle(dfft(max_f(1),:));
for k=1:numAnt
    calibPhase(k) = dtmp(1) - dtmp(k);
end

for k=1:numAnt
    din_calibFreqAmpPhase(:, k) = din_calibFreqAmp(:, k) * exp(1j * calibPhase(k));
end
dfft = fft(din_calibFreqAmpPhase, ptTimes * ptRange, 1);
dabs = abs(dfft);
figure(fig); fig=fig+1;
subplot(2,1,1);
for k=1:numAnt
    plot(dabs(:, k)); hold on;
end
hold off;
title('freq domain after calib phase');
legend('1','2','3','4','5','6','7','8','9','10','11','12');

subplot(2,1,2);
for k=1:numAnt
    plot(angle(dfft(:,k))); hold on;
end
hold off;
title('freq domain after calib phase');
legend('1','2','3','4','5','6','7','8','9','10','11','12');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% printFile(1, 'real', [calibCfg.savePath, 'calib_freq.txt'],  1, 2, 15, 2, 15, calibFreq);
% printFile(1, 'real', [calibCfg.savePath, 'calib_amp.txt'],   1, 2, 15, 2, 15, calibAmp);
% printFile(1, 'real', [calibCfg.savePath, 'calib_phase.txt'], 1, 2, 15, 2, 15, calibPhase);
  amp_calib = calibAmp.';
 freq_calib = calibFreq.';
phase_calib = calibPhase.';
save('amp_calib.mat',   'amp_calib');
save('freq_calib.mat',  'freq_calib');
save('phase_calib.mat', 'phase_calib');

end
